﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Sunny.UI;

namespace project
{
    public partial class RentBook : UIForm
    {
        public RentBook()
        {
            InitializeComponent();
        }
        public RentBook(string book)
        {
            InitializeComponent();
            uiTextBox2.Text = book;
        }

        private void uiTextBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void uiButton1_Click(object sender, EventArgs e)
        {
            //借书确认
            string stu_num = uiTextBox1.Text.Trim();
            string book_num = uiTextBox2.Text.Trim();
            if (string.IsNullOrEmpty(stu_num) || string.IsNullOrEmpty(book_num))
            {
                MessageBox.Show("请填写完整信息", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                string sql1 = $"select repertory from books where number={book_num};";
                string sql2 = $"select count(*) from student where number={stu_num};";
                int result1 = (int)SqlHelper.ExecuteScalar(sql1);
                long result2 = (long)SqlHelper.ExecuteScalar(sql2);
                if(result2 == 0)
                {

                        MessageBox.Show("学生信息不存在", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                if (result1 > 0)
                {
                    string sqlStr = $"call 'rent_book'({stu_num},{book_num});";
                    int result = (int)SqlHelper.rent_books(stu_num, book_num);
                    if (result != 0)
                    {
                        MessageBox.Show("借书成功", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);

                    }
                    else
                    {
                        MessageBox.Show("借书失败", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }

                }
                else
                {
                    MessageBox.Show("此书库存不足", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }

                }
            }
        }

        private void uiButton2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
